import js from '@eslint/js'
import globals from 'globals'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'
import prettierPlugin from 'eslint-plugin-prettier'
import vuePrettierConfig from '@vue/eslint-config-prettier'
import json from '@eslint/json'
import markdown from '@eslint/markdown'
import css from '@eslint/css'
import { defineConfig } from 'eslint/config'

export default defineConfig([
  {
    files: ['**/*.{js,mjs,cjs,ts,mts,cts,vue}'],
    plugins: { js },
    extends: ['js/recommended'],
    languageOptions: { globals: globals.browser },
    ignores: ['**/packages/manage/', '**/packages/server/']
  },
  tseslint.configs.recommended,
  ...pluginVue.configs['flat/recommended'].map((config) => ({
    ...config,
    files: ['**/*.vue']
  })),
  { files: ['**/*.vue'], languageOptions: { parserOptions: { parser: tseslint.parser } } },
  { files: ['**/*.json'], plugins: { json }, language: 'json/json', extends: ['json/recommended'] },
  {
    files: ['**/*.jsonc'],
    plugins: { json },
    language: 'json/jsonc',
    extends: ['json/recommended']
  },
  {
    files: ['**/*.md'],
    plugins: { markdown },
    language: 'markdown/gfm',
    extends: ['markdown/recommended']
  },
  { files: ['**/*.css'], plugins: { css }, language: 'css/css', extends: ['css/recommended'] },
  {
    ignores: [
      'node_modules/',
      '**/dist/',
      '**/.vitepress/',
      '**/docs/src/**.md',
      '**/docs/src/public/',
      '**/packages/manage/',
      '**/packages/server/'
    ]
  },

  // 集成 prettier，需要放在配置数组的最后
  {
    // 注册 prettier 插件
    plugins: {
      prettier: prettierPlugin
    },
    rules: {
      // 展开 @vue/eslint-config-prettier 的规则
      ...vuePrettierConfig.rules,

      // 启用 eslint-plugin-prettier 提供的规则，其错误将作为 eslint 错误报告
      'prettier/prettier': 'error'
    }
  }
])
